package com.yonyou.le.unattended.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.google.gson.Gson;
import com.yonyou.le.unattended.cache.DeviceCacheManager;
import com.yonyou.le.unattended.cache.MonitorCacheManager;
import com.yonyou.le.unattended.cache.MonitorCacheTypeEnum;
import com.yonyou.le.unattended.cache.entity.DeviceData;
import com.yonyou.le.unattended.cache.entity.OPCache;
import com.yonyou.le.unattended.delivery.entity.AdaptorParamVO;
import com.yonyou.le.unattended.delivery.entity.DeliveryParamVO;
import com.yonyou.le.unattended.delivery.entity.DevGroupParamVO;
import com.yonyou.le.unattended.delivery.entity.DeviceParamVO;
import com.yonyou.le.unattended.delivery.entity.OperatPointParamVO;
import com.yonyou.le.unattended.entity.meta.CarInfoData;
import com.yonyou.le.unattended.entity.meta.OperationPoint;
import com.yonyou.le.unattended.repository.meta.OperationMonitorDao;
import com.yonyou.le.unattended.util.CalServletForLE;
import com.yonyou.le.unattended.util.CommonUtils;
import com.yonyou.le.unattended.util.ProConstant;
import com.yonyou.tool.servlet.NCConnService;

@Component
public class OperationMonitorService {
	private Logger logger = LoggerFactory.getLogger(OperationMonitorService.class);
//	private static final String attr_oppk="pk_operpoint";
//	private static final String attr_opcode="hcopercode";
//	private static final String attr_opname="hcopername";
	@Autowired
	private OperationMonitorDao dao;
	@Autowired
	private MonitorCacheManager mCacheManager; 
	@Autowired
	private DeviceCacheManager dCacheManager; 
	@Autowired
	private ProConstant constant;
	/**
	 * 查询作业点列表
	 * 
	 * @return
	 */
	public List<OperationPoint> findByUser() {

		logger.debug("OperationPoint execute findByUser when data is less");
		List<OperationPoint> ops = new ArrayList<OperationPoint>();
		OperationPoint op1 = new OperationPoint();

		op1.setId("a1");
		op1.setOpname("东区一磅");
		op1.setOpcode("ZYD001");
		op1.setIschecked("checked");

		ops.add(op1);

		OperationPoint op2 = new OperationPoint();

		op2.setId("a2");
		op2.setOpname("东区二磅");
		op2.setOpcode("ZYD002");
		op2.setIschecked("1");

		ops.add(op2);

		OperationPoint op3 = new OperationPoint();

		op3.setId("a3");
		op3.setOpname("东区三磅");
		op3.setOpcode("ZYD003");
		op3.setIschecked("0");

		ops.add(op3);

		OperationPoint op4 = new OperationPoint();

		op4.setId("b1");
		op4.setOpname("西区一磅");
		op4.setOpcode("ZYD004");
		op4.setIschecked("0");

		ops.add(op4);

		OperationPoint op5 = new OperationPoint();

		op5.setId("b2");
		op5.setOpname("西区二磅");
		op5.setOpcode("ZYD005");
		op5.setIschecked("1");

		ops.add(op5);

		OperationPoint op6 = new OperationPoint();

		op6.setId("b3");
		op6.setOpname("西区三磅");
		op6.setOpcode("ZYD006");
		op6.setIschecked("0");

		ops.add(op6);
		
		return ops;
	}
	
	/**
	 * 查询作业点实时车辆信息
	 * @param opcode
	 * @return
	 */
	public CarInfoData selectMonitorData(String opcode){
		CarInfoData carInfo = mCacheManager.getCacheData(MonitorCacheTypeEnum.CARINFO, opcode, CarInfoData.class);
		if(carInfo==null){
			carInfo=new CarInfoData();
		}
		return carInfo;	
		
		
	}
	/**
	 * 查询所有作业点
	 * @return
	 */
	public List<OperationPoint> findAll() {
		OPCache opCache = dCacheManager.getOPCache(DeviceCacheManager.LIST_OP, OPCache.class);
		return opCache.getOpList();
	}
	/*public List<OperationPoint> findAll() {
		logger.debug("Find All OperationPoint");
		List<OperationPoint> ops = new ArrayList<OperationPoint>();
		
		NCConnService client = new NCConnService(ip,port);
		String[] pks={};
		//查询返回jsonarray
		String jsonString = (String) client.callNCService(CalServletForLE.getMonitorPointContext(pks), null, null);
		JSONArray jsonArray =  JSONArray.fromObject(jsonString);
		for (int i = 0; i < jsonArray.size(); i++) {
			JSONObject jo = jsonArray.getJSONObject(i);
			JSONObject joop= jo.getJSONObject("parent");
			OperationPoint op = new OperationPoint();
			op.setId(joop.getString(attr_oppk));
			op.setOpname(joop.getString(attr_opname));
			op.setOpcode(joop.getString(attr_opcode));
			op.setIschecked("0");
			ops.add(op);
		}
		return ops;
	}*/
	
	
	
	
	/**
	 * 测试接口
	 * @param args
	 */
	public static void main(String[] args) {
		
		NCConnService client = new NCConnService("10.1.211.153",8081);
		String jsonString = (String) client.callNCService(CalServletForLE.getDeliveryQueryContext(), null, null);
		//JSONObject EXEVOjsonObject = JSONObject.fromObject(jsonString);
		System.out.println(jsonString);
		
		Gson gs = new Gson();
		DeliveryParamVO data = gs.fromJson(jsonString, DeliveryParamVO.class);
		System.out.println(data.getAdaptorParamList().size());
		System.out.println(data.getDevGroupParamList().size());
		System.out.println(data.getDeviceParamList().size());
		System.out.println(data.getOperaPointParamList().size());
		
//		Map<String,DeviceParamVO> deviceMap = CommonUtils.list2Map(data.getDeviceParamList(), "hcdevcode");
//		Map<String,DevGroupParamVO> devGroupMap = CommonUtils.list2Map(data.getDevGroupParamList(),"hcdevgroupcode");
//		Map<String, AdaptorParamVO> adaptorMap = CommonUtils.list2Map(data.getAdaptorParamList(),"bcdeviceid");		
//		Map<String, List<OperatPointParamVO>> operaPointMap = CommonUtils.list2Maplist(data.getOperaPointParamList(),"bcdeviceid");
		
//		for (DeviceParamVO device : deviceMap.values()) {
//			String devGroupCode=device.getHcdevgroup();
//			DevGroupParamVO devGroupParamVO = devGroupMap.get(devGroupCode);
//			AdaptorParamVO adaptorParamVO = adaptorMap.get(device.getPk_device());
//			List<OperatPointParamVO> operatPointParamVOs = operaPointMap.get(device.getPk_device());
//			DeviceData deviceData = new DeviceData(device, devGroupParamVO, adaptorParamVO, operatPointParamVOs);
			
//		}
		
	}
	
}
